package com.ysyc.driver_ee.activity;


import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.NonNull;


import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.tools.PermissionUtils;
import com.ysyc.driver_ee.tools.ToastUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;


public class SplashActivity extends BaseActivity {

    private Disposable subscribe;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        subscribe = Observable.create((ObservableOnSubscribe<Boolean>) e -> e.onNext(true)).debounce(1, TimeUnit.SECONDS)
                .subscribe(aBoolean -> nextAct());
    }



    private void nextAct() {
        String[] permisson = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS};
        if (isLogin()) {
            PermissionUtils.requestPermissions(this, 1, permisson, mPermissionGrant);
        } else {
            PermissionUtils.requestPermissions(this, 2, permisson, mPermissionGrant);
        }

    }

    @Override
    protected void onDestroy() {
        if (subscribe != null && !subscribe.isDisposed()) {
            subscribe.dispose();
        }
        super.onDestroy();
    }

    private PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case 1:
                    obtainCapacity();
//                    initNetworkCallback();
                    break;
                case 2:
                    finish();
                    toActivityClearTask(LoginActivity.class);
                    break;
                default:
                    break;
            }
        }

        private void obtainCapacity() {
            NetworkCallback<DriverInfo>  tNetworkCallback = new NetworkCallback<>(new HttpResource<DriverInfo>() {
                @Override
                public void onSuccess(DriverInfo driverInfo) {
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    intent.putExtra(VerificationCodeActivity.DRIVER_INFO,driverInfo);
                    startActivity(intent);
                    finish();
                }

                @Override
                public void onFailure(String errorDes) {
                    ToastUtils.show(errorDes);

                }

                @Override
                public void onLoading(Dialog dialog) {

                }
            });
            Observable<DriverInfo> driverInfoObservable = RetrofitClient.getInstance().getHttpServices().obtainDriverInfo();
            tNetworkCallback.postRequest(driverInfoObservable, null);
        }
    };



    private void initNetworkCallback() {
        toActivityClearTask(DriverCertificationActivity.class);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtils.requestPermissionResult(this, requestCode, permissions, grantResults, mPermissionGrant);
    }

}
